﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_CustomerManager : System.Web.UI.Page
{
    public static int cusID;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!checkLoginAdmin())
            {
                ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script> alert('You have to login as Admin to access this page!');</script>");
                Response.Redirect("Login.aspx?prePage=cusManager");
            }
            LoadData();
            
            //Sort
            if (GridView1.Rows.Count > 0)
            {
                GridView1.UseAccessibleHeader = true;
                GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
                //GridView1.FooterRow.TableSection = TableRowSection.TableFooter;
            }
            else
                ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script> alert('Have not customer!');</script>");
        }
    }
    // check login
    public bool checkLoginAdmin()
    {
        // check session
        if (Session["username"] != null)
        {
            return true;
        }
        else
        {
            // check cookie 
            if (Request.Cookies["username"] != null)
            {
                Session["username"] = Request.Cookies["username"].Value;
                Session["fullname"] = Request.Cookies["fullname"].Value;
                return true;
            }

        }
        return false;
    }
    private void LoadData()
    {
        DataTable dt = CusManagement.GetAllCus().Tables["AllCus"];
        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        MultiView1.ActiveViewIndex = 0;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        LoadData();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        cusID = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        //Delete OrderTour
        OrderTourManagement.DeleteOrderTourFromCus(cusID);
        //Delte Customer
        if (CusManagement.DeleteCus(cusID))
            LoadData();
    }
}